﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.Xml
Imports System.Xml.XPath
Imports System.Web
Imports System.Text

Namespace Rss

    ''' <summary>
    ''' Rss 2.0 channel definition
    ''' </summary>
    ''' <remarks>
    ''' textBox, skipHours, skipDays have been excluded
    ''' </remarks>
    Public Class RssChannel
        Inherits RssBase

#Region "Constructors"


        Public Sub New(ByVal title As String, ByVal link As Uri, ByVal description As String)
            Me.title = title
            Me.link = link
            Me.description = description
            Me.items = New List(Of RssItem)
        End Sub

        Public Sub New(ByVal title As String, ByVal link As Uri, ByVal description As String, ByVal items As List(Of RssItem))
            Me.title = title
            Me.link = link
            Me.description = description
            Me.items = items
        End Sub

        Public Sub New(ByVal title As String, ByVal link As Uri, ByVal description As String, _
                       ByVal language As String, ByVal copyright As String, ByVal managingEditor As String, _
                       ByVal webMaster As String, ByVal pubDate As DateTime, ByVal lastBuildDate As DateTime, _
                       ByVal category As List(Of RssCategory), ByVal docs As String, _
                       ByVal cloud As RssCloud, ByVal ttl As Integer, ByVal image As String, ByVal rating As String, ByVal items As List(Of RssItem))

            Me.title = title
            Me.link = link
            Me.description = description
            If Not String.IsNullOrEmpty(language) Then Me.language = language
            Me.copyright = copyright
            Me.managingEditor = managingEditor
            Me.webMaster = webMaster
            Me.pubDate = pubDate
            Me.lastBuildDate = lastBuildDate
            Me.category = category
            If Not String.IsNullOrEmpty(docs) Then Me.docs = docs
            Me.cloud = cloud
            Me.ttl = ttl
            Me.image = image
            Me.rating = rating
            Me.items = items

        End Sub


#End Region

        Public Sub AddExtension(ByVal extension As RssExtension)
            If extensions Is Nothing Then extensions = New List(Of RssExtension)
            extensions.Add(extension)
        End Sub

        Public Sub AddCategory(ByVal cat As RssCategory)
            If category Is Nothing Then category = New List(Of RssCategory)
            category.Add(cat)
        End Sub

        ''' <summary>
        ''' Returns an instantiation of RssChannel from the xml channel node in the rss 2.0 feed
        ''' </summary>
        Public Shared Function GetRssChannel(ByVal item As XmlNode, Optional ByVal max_items As Integer = 0) As RssChannel

            Dim items As New List(Of RssItem)
            Dim itemList As XmlNodeList = item.SelectNodes("channel/item")
            If itemList.Count > 0 Then
                Dim n As XmlNode = Nothing
                Dim count As Integer = 0
                For Each n In itemList
                    Dim newitem As RssItem = RssItem.GetRssItem(n)
                    If newitem.link IsNot Nothing Then items.Add(newitem)
                    If max_items > 0 Then count += 1
                    If count = max_items And max_items > 0 Then Exit For
                Next
            End If

            Dim channelItem As XmlNode = item.SelectSingleNode("channel")

            Return New RssChannel( _
                GetChildItemInnerText(channelItem, "title"), _
                GetChildItemInnerTextUri(channelItem, "link"), _
                GetChildItemInnerText(channelItem, "description"), _
                GetChildItemInnerText(channelItem, "language"), _
                GetChildItemInnerText(channelItem, "copyright"), _
                GetChildItemInnerText(channelItem, "managingEditor"), _
                GetChildItemInnerText(channelItem, "webMaster"), _
                GetChildItemInnerTextDateTime(channelItem, "pubDate"), _
                GetChildItemInnerTextDateTime(channelItem, "lastBuildDate"), _
                RssCategory.GetRssCategories(channelItem), _
                GetChildItemInnerText(channelItem, "docs"), _
                RssCloud.GetRssCloud(channelItem), _
                GetChildItemInnerTextInteger(channelItem, "ttl"), _
                GetChildItemInnerText(channelItem, "image"), _
                GetChildItemInnerText(channelItem, "rating"), _
                items)

        End Function


        Public Function ToXmlString() As String
            Dim sb As New StringBuilder
            sb.Append("<channel>")
            sb.Append(MakeItem("title", title))
            sb.Append(MakeItem("link", link.ToString))
            sb.Append(MakeItem("description", description))
            sb.Append(MakeItem("language", language))
            sb.Append(MakeItem("copyright", copyright))
            sb.Append(MakeItem("managingEditor", managingEditor))
            sb.Append(MakeItem("webMaster", webMaster))
            sb.Append(MakeItem("pubDate", pubDate.ToString("r")))
            sb.Append(MakeItem("lastBuildDate", lastBuildDate.ToString("r")))
            If category IsNot Nothing Then
                Dim cat As RssCategory = Nothing
                For Each cat In category
                    sb.Append(cat.ToXmlString)
                Next
            End If
            sb.Append(MakeItem("generator", generator))
            sb.Append(MakeItem("docs", docs))
            If cloud IsNot Nothing Then sb.Append(cloud.ToXmlString)
            If ttl > 0 Then sb.Append(MakeItem("ttl", ttl.ToString))
            sb.Append(MakeItem("image", image))
            sb.Append(MakeItem("rating", rating))

            If extensions IsNot Nothing Then
                ' add the extensions
                Dim ext As RssExtension = Nothing
                For Each ext In extensions
                    sb.Append(ext.ToXmlString)
                Next
            End If

            If items IsNot Nothing Then

                Dim item As RssItem = Nothing
                For Each item In items
                    sb.Append(item.ToXmlString)
                Next

            End If

            sb.Append("</channel>")

            Return sb.ToString
        End Function

       

#Region "Properties"

        ''' <summary>
        ''' The title of the channel
        ''' </summary>
        Private _title As String
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        ''' <summary>
        ''' The url of the website corresponding to the channel
        ''' </summary>
        Private _link As Uri
        Public Property link() As Uri
            Get
                Return _link
            End Get
            Set(ByVal value As Uri)
                _link = value
            End Set
        End Property

        ''' <summary>
        ''' Abstract or short description of the channel
        ''' </summary>
        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property

        ''' <summary>
        ''' The language the channel is written in. (optional)
        ''' </summary>
        Private _language As String = "en-us"
        Public Property language() As String
            Get
                Return _language
            End Get
            Set(ByVal value As String)
                _language = value
            End Set
        End Property

        ''' <summary>
        ''' Copyright notice for content in the channel. (optional)
        ''' </summary>
        Private _copyright As String
        Public Property copyright() As String
            Get
                Return _copyright
            End Get
            Set(ByVal value As String)
                _copyright = value
            End Set
        End Property

        ''' <summary>
        ''' Email address for person responsible for editorial content (optional)
        ''' </summary>
        Private _managingEditor As String
        Public Property managingEditor() As String
            Get
                Return _managingEditor
            End Get
            Set(ByVal value As String)
                _managingEditor = value
            End Set
        End Property

        ''' <summary>
        ''' Email address for person responsible for technical issues relating to channel (optional)
        ''' </summary>
        Private _webMaster As String
        Public Property webMaster() As String
            Get
                Return _webMaster
            End Get
            Set(ByVal value As String)
                _webMaster = value
            End Set
        End Property

        ''' <summary>
        ''' The publication date for the content in the channel (optional)
        ''' </summary>
        Private _pubDate As DateTime
        Public Property pubDate() As DateTime
            Get
                Return _pubDate
            End Get
            Set(ByVal value As DateTime)
                _pubDate = value
            End Set
        End Property

        ''' <summary>
        ''' The last time the content of the channel changed. (optional)
        ''' </summary>
        Private _lastBuildDate As DateTime
        Public Property lastBuildDate() As DateTime
            Get
                Return _lastBuildDate
            End Get
            Set(ByVal value As DateTime)
                _lastBuildDate = value
            End Set
        End Property

        ''' <summary>
        ''' A list of one or more categories that the channel belongs to (optional)
        ''' </summary>
        Private _category As List(Of RssCategory)
        Public Property category() As List(Of RssCategory)
            Get
                Return _category
            End Get
            Set(ByVal value As List(Of RssCategory))
                _category = value
            End Set
        End Property

        ''' <summary>
        ''' A string indicating the program used to generate the channel (optional)
        ''' </summary>
        Private _generator As String = "Mindfly Web Studio's Boogaloo 1.0.0.0 (http://www.mindfly.com)"
        Public ReadOnly Property generator() As String
            Get
                Return _generator
            End Get
        End Property

        ''' <summary>
        ''' A URL that points to the documentation for the format used in the RSS file (optional)
        ''' </summary>
        Private _docs As String = "http://www.rssboard.org/rss-specification"
        Public Property docs() As String
            Get
                Return _docs
            End Get
            Set(ByVal value As String)
                _docs = value
            End Set
        End Property

        ''' <summary>
        ''' Webservice cloud for channel updates (optional)
        ''' </summary>
        Private _cloud As RssCloud
        Public Property cloud() As RssCloud
            Get
                Return _cloud
            End Get
            Set(ByVal value As RssCloud)
                _cloud = value
            End Set
        End Property

        ''' <summary>
        ''' Time to live in minutes (optional)
        ''' </summary>
        ''' <remarks></remarks>
        Private _ttl As Integer
        Public Property ttl() As Integer
            Get
                Return _ttl
            End Get
            Set(ByVal value As Integer)
                _ttl = value
            End Set
        End Property

        ''' <summary>
        ''' GIF, JPEG or PNG image that can be displayed with the channel
        ''' </summary>
        Private _image As String
        Public Property image() As String
            Get
                Return _image
            End Get
            Set(ByVal value As String)
                _image = value
            End Set
        End Property


        ''' <summary>
        ''' PICS rating - http://www.w3.org/PICS/ (optional)
        ''' </summary>
        Private _rating As String
        Public Property rating() As String
            Get
                Return _rating
            End Get
            Set(ByVal value As String)
                _rating = value
            End Set
        End Property


        ''' <summary>
        ''' List of items in this feed
        ''' </summary>
        Private _items As List(Of RssItem)
        Public Property items() As List(Of RssItem)
            Get
                Return _items
            End Get
            Set(ByVal value As List(Of RssItem))
                _items = value
            End Set
        End Property


        Private _extensions As List(Of RssExtension)
        Public Property extensions() As List(Of RssExtension)
            Get
                Return _extensions
            End Get
            Set(ByVal value As List(Of RssExtension))
                _extensions = value
            End Set
        End Property


#End Region

    End Class


End Namespace